﻿using NetMsSqlLibrary;
using System;
using System.Data;
using System.Net;
using System.Web.UI;
using Newtonsoft.Json;

namespace NetMsSqlTest
{
    public partial class index : System.Web.UI.Page
    {
        /// <summary>
        /// 封装弹出消息的方法
        /// </summary>
        /// <param name="mes"></param>
        public void MessageBoxShow(string mes)
        {
            ClientScriptManager scriptManager = ((Page)System.Web.HttpContext.Current.Handler).ClientScript;
            scriptManager.RegisterStartupScript(typeof(string), "", "alert('" + mes + "');", true);
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlManager sqlManager = new SqlManager();
            try
            {
                
                WhereList where = new WhereList();
                where.add("USER_NAME", WhereList.Comparison.等于, "供应商");
                where.add("USER_ROLE", WhereList.Comparison.等于, "VENDER001");
                DataTable dt = sqlManager.SelectData("coreUser",where);
                MessageBoxShow(dt.Rows.Count.ToString());
            }
            catch (Exception ex)
            {
                MessageBoxShow(ex.Message);
            }

        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnINSERTINTO_Click(object sender, EventArgs e)
        {
            Hanlder hanlder = new Hanlder();
            hanlder.HanlderBegin();
            SqlManager sqlManager = new SqlManager();
            try
            {
                DataTable dt = Tools.CreatDataTableQuick("coreUser");
                DataRow dr;
                dr = dt.NewRow();
                dt.Rows.Add(dr);
                dt.Rows[0]["USER_NAME"] = "moss测试005";
                dt.Rows[0]["USER_ID"] = "mosstest005";
                dr = dt.NewRow();
                dt.Rows.Add(dr);
                dt.Rows[1]["USER_NAME"] = "moss测试006";
                dt.Rows[1]["USER_ID"] = "mosstest006";
                int number = sqlManager.InsertData(hanlder.cmd, "coreUser",dt);
                hanlder.HanlderCommit(); //提交事务
                MessageBoxShow(number.ToString());
            }
            catch (Exception ex)
            {
                hanlder.HanlderRollback(); //回滚事务
                MessageBoxShow(ex.Message);
            }
            finally
            {
                hanlder.HanlderFinish();
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnUPDATA_Click(object sender, EventArgs e)
        {
            Hanlder hanlder = new Hanlder();
            hanlder.HanlderBegin();
            SqlManager sqlManager = new SqlManager();
            try
            {
                WhereList where = new WhereList();
                where.add("USER_ID",WhereList.Comparison.等于,"mosstest001");
                DataTable dt = Tools.CreatDataTableQuick("coreUser");
                DataRow dr = dt.NewRow();
                dr["USER_NAME"] = "moss测试001";
                dr["USER_ROLE"] = "0";
                int number = sqlManager.UpData(hanlder.cmd, "coreUser", dr, where);
                hanlder.HanlderCommit(); //提交事务
                MessageBoxShow(number.ToString());
            }
            catch (Exception ex)
            {
                hanlder.HanlderRollback(); //回滚事务
                MessageBoxShow(ex.Message);
            }
            finally
            {
                hanlder.HanlderFinish();
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnDELETE_Click(object sender, EventArgs e)
        {
            Hanlder hanlder = new Hanlder();
            hanlder.HanlderBegin();
            SqlManager sqlManager = new SqlManager();
            try
            {
                WhereList where = new WhereList();
                where.add("USER_ID", WhereList.Comparison.等于, "mosstest006");
                int number = sqlManager.DeleteData(hanlder.cmd, "coreUser",where);
                hanlder.HanlderCommit(); //提交事务
                MessageBoxShow(number.ToString());
            }
            catch (Exception ex)
            {
                hanlder.HanlderRollback(); //回滚事务
                MessageBoxShow(ex.Message);
            }
            finally
            {
                hanlder.HanlderFinish();
            }
        }

        /// <summary>
        /// 根据表名创建Datatable
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CreatDataTable_Click(object sender, EventArgs e)
        {
            DataTable dt = Tools.CreatDataTableQuick("coreUser");
        }

        protected void BtnAPI_Click(object sender, EventArgs e)
        {
            //string serviceAddress = "http://10.10.10.5/api/values";
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
            //request.Method = "GET";
            //request.ContentType = "text/html;charset=UTF-8";
            //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //Stream myResponseStream = response.GetResponseStream();
            //StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            //string retString = myStreamReader.ReadToEnd();
            //myStreamReader.Close();
            //myResponseStream.Close();
            //Response.Write(retString);

            //using (var client = new WebClient())
            //{
            //    var responseString = client.DownloadString("http://localhost:2786/api/values");
            //}

            WebClient WC = new WebClient();
            WC.Headers.Add("Content-Type", "application/json");
            var Res = WC.UploadString("http://localhost:2786/api/values", JsonConvert.SerializeObject(""));
        }
    }
}